function onModuleLoad() {
	$("#accordion").accordion('activate', 1);
	var $districtModule = $("#districtModule");
	var $districtModuleSearch = $("#districtModule_search_button",$districtModule);
	
	$districtModuleSearch.click(function(){
		 var stateId = "";
	 	 $("select option:selected").each(function () {
	         stateId = $(this).attr('value');
	      });
	 	var url = "../district/state/" + stateId;
	 	window.location = url;
	});
};
	
function onDistrictListLoad() {
	var $districtModule = $("#districtModule");
	var $district_add_form = $("#districtModule_add_form");
	var $districtTable = $("#districtModule_table",$districtModule);
	var $serverErrormessage;
	var deleteDistrictName;
	var deleteDistrictUrl;
	var deleteDistrictId;
	var districtRowToDelete;
	// following script stops the submit event of form when user clicks the Enter button
	$('.noEnterSubmit').keypress(function(e){
	  	if ( e.which == 13 ) return false;
	  	
	});
	var $district_add_dialog = $( "#districtModule_add_dialog" ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Add: function() {
				if($district_add_form.valid())
				{
				    var url = $district_add_form.attr( 'action' );
				    var jqxhr = $.post(url,$district_add_form.serialize(), function(data) {
				   			
							  if(data.status == "Success"){
								  $districtTable.append("<tr> <td>" +data.resultMap.newDistrictName + "</td> <td><a class=\"districtModuleDeleteDistrict\" href=\"../delete/$" +data.resultMap.newDistrictId +"\">Delete</a></td> </tr>");
									 
								  $district_add_dialog.dialog( "close" );
								  
								  $("#districtModule p").remove();
								  
							  }
					      else {
					    	  $serverErrormessage = $( "#districtModule_add_dialog_status" , $district_add_dialog);
					    	  $serverErrormessage.text("Error adding new district - " + data.errorMap.errorMessage);
					      }					   	  
					    }, "json");					
				}
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$tab_title_input.focus();
			$serverErrormessage.text("");
		},
		close: function() {
			$district_add_form[ 0 ].reset();
		}
	});
	
	// addTab button: just opens the dialog
	$( "#districtModule_add_button" ).click(function() {
		$district_add_dialog.dialog( "open" );
		});
	
	var $district_delete_dialog = $("#districtModule_delete_dialog", $districtModule ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Yes: function() {
				var jqxhrDelete = $.post(deleteDistrictUrl,{districtId:deleteDistrictId}, function(data) {
					if(data.status == "Success"){
						  districtRowToDelete.remove();
						  $district_delete_dialog.dialog( "close" );
				    	
				      }
				      else {
				    	  $serverErrormessage = $( "#districtModule_delete_dialog_status" , $district_delete_dialog);
				    	  $serverErrormessage.text("Error deleting the district - " + data.errorMap.errorMessage);
				      }
				    	  
				    }, "json");		
						
			},
			No: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			//$tab_title_input.focus();
		},
		close: function() {
			$serverErrormessage.text("");
		}
	});
	
	var $districtDeleteLink = $("a", $districtTable);
	$districtDeleteLink.live("click",function(event) {
        event.preventDefault();
        districtRowToDelete = $(this).parent().parent();
        deleteDistrictName = $(this).parent().siblings().html();
        deleteDistrictUrl = $(this).attr('href');
        var hrefArray= deleteDistrictUrl.split('$'); 
        deleteDistrictUrl = hrefArray[0];
        deleteDistrictId = hrefArray[1];
        $("#districtModule_delete_message",$district_delete_dialog).text(deleteDistrictName);
        $district_delete_dialog.dialog( "open" );
       
    });
};